package com.catsic.mod.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.catsic.base.BaseServiceImpl;
import com.catsic.base.exception.CatsicException;
import com.catsic.mod.condition.SendMessageCondition;
import com.catsic.mod.convert.Converts;
import com.catsic.mod.dao.SendMessageDao;
import com.catsic.mod.entity.SendMessage;
import com.catsic.mod.exception.CatsicExceptionCode;
import com.catsic.mod.form.SendMessageForm;
import com.catsic.mod.service.SendMessageService;
import com.catsic.mod.specification.SendMessageSpec;

/**
 * 站内信息发件 service实现类
 */
@Service("SendMessageService")
public class SendMessageServiceImpl extends BaseServiceImpl<SendMessage, String> implements SendMessageService{

	@Autowired
	private SendMessageDao sendMessageDao;
	

	
	@Override
	public SendMessage findById(String id) {
		SendMessage sendMessage=super.getById(id);
		if(sendMessage==null){
			throw new CatsicException(CatsicExceptionCode.站内邮件提醒);
		}
		return sendMessage;
	}
	
	@Override
	public Page<SendMessageForm> findAll(SendMessageCondition condition,Pageable pageable) {
		SendMessageSpec spec = new SendMessageSpec(condition);
		Page<SendMessage> page = sendMessageDao.findAll(spec, pageable);
		List<SendMessageForm> listSendMessageForm = new ArrayList<SendMessageForm>();
		for (SendMessage sendMessage : page.getContent()) {
			listSendMessageForm.add(Converts.convert(sendMessage));
		}
		Page<SendMessageForm> page_ = new PageImpl<SendMessageForm>(listSendMessageForm, pageable, page.getTotalElements());
		return page_;
	}
	
	@Override
	public SendMessage add(SendMessageForm sendMessageForm){
		SendMessage sendMessage=new SendMessage();
		sendMessage.setTitle(sendMessageForm.getTitle());
		sendMessage.setContent(sendMessageForm.getContent());
		sendMessage.setUser(sendMessageForm.getUser());
		sendMessage.setRemind(sendMessageForm.getRemind());
		return super.add(sendMessage);
	}

	/*public void bind (String sendMessageId,String[] receiveMessageIds){
		SendMessage sendMessage=findById(sendMessageId);
		List<ReceiveMessage> list=new ArrayList<ReceiveMessage>();
		for (String receiveMessageId : receiveMessageIds) {
			ReceiveMessage receiveMessage = new ReceiveMessage();
			receiveMessage.setId(receiveMessageId);
			list.add(receiveMessage);
		}
		sendMessage.setReceiveMessages(list);
		super.update(sendMessage);
	}*/
}
